****
*Authors: M. Kenwick and D. Lemke
*Purpose: Replicate analysis in Kenwick and Lemke, BJPS, 2022
*Date: Jun 15, 2021
****
clear
version 15



********************************************************************************
**Set working directory and load data
********************************************************************************
*Change working directory to folder storing the replication data
cd ""
use "TCSyears_duration_bjps.dta", clear




********************************************************************************
**Tables 1 and 2
********************************************************************************
*Note: Checks of proportional hazards assumption are commented out
*but can be run by removing /* and */ from the code below


**Table 1, Model 1
/*
*PHA check: 
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox     Host_ThomRivals latentmean ///
	,efron shared(tc_ccode) frailty(gamma)
estat phtest, detail log
*Violations: Host_ThomRivals
*/
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean, efron ///
	tvc(Host_ThomRivals) texp(ln(_t)) ///
	shared(tc_ccode) frailty(gamma) nohr 
lincom latentmean*.924, hr


**Table 2, Model 1
/*
*PHA check: 
stset counter, failure(Death_Type==2) id(tc_ccode)
stcox     Host_ThomRivals latentmean ///
	,efron shared(tc_ccode) frailty(gamma)
estat phtest, detail log
*Violations: None
*/

stset counter, failure(Death_Type==2) id(tc_ccode)
stcox Host_ThomRivals latentmean, efron ///
	shared(tc_ccode) frailty(gamma) nohr 	

	
*Table 1, Model 2

/*
*PHA ceck: 
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains  ///
	,efron shared(tc_ccode) frailty(gamma) nohr
estat phtest, detail log
*Violations: Rivalry at p<.1

*Model without correction (footnote 17)
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains  ///
	,efron  shared(tc_ccode) frailty(gamma) nohr
*/
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains  ///
	,efron  shared(tc_ccode) frailty(gamma) nohr ///
	tvc(Host_ThomRivals) texp(ln(_t))
lincom latentmean*.924, hr



*Table 2, Model 2
/*
*PHA Check
*NOTE: diagnostic model run without xconst due to convergence issues
stset counter, failure(Death_Type==2) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains ///
	,efron shared(tc_ccode) frailty(gamma) nohr
estat phtest, detail log
Violations: Host_cinc_1000 num_tcs
*/
stset counter, failure(Death_Type==2) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains , efron  ///
	tvc(Host_cinc_1000 num_tcs) ///
	texp(ln(_t)) nohr shared(tc_ccode) frailty(gamma)	 
	 

*Table 1, Model 3
/*
*PHA Check
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 num_tcs ///
	Mountains Host_sf Host_Devel log_Pop ///
	Host_Alliances Support Recognition ///
	Fragment xconst Loot Host_contig_land ///
	,efron shared(tc_ccode) frailty(gamma)	 
estat phtest, detail log
*Violations: latentmean, xconst, Host_contig_land
*/
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Support Recognition ///
	Fragment xconst Mountains Loot Host_contig_land ,efron  ///
	tvc(latentmean Host_contig_land xconst) ///
	texp(ln(_t)) nohr shared(tc_ccode) frailty(gamma)
lincom latentmean*.924, hr


*Table 2, Model 3
/*
*PHA Check
stset counter, failure(Death_Type==2) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 num_tcs ///
	Mountains Host_sf Host_Devel log_Pop ///
	Host_Alliances Support Recognition ///
	Fragment xconst Loot Host_contig_land ///
	,efron shared(tc_ccode) frailty(gamma)	 
estat phtest, detail log
Violations: None
*/
stset counter, failure(Death_Type==2) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 num_tcs ///
	Mountains Host_sf Host_Devel log_Pop ///
	Host_Alliances Support Recognition ///
	Fragment xconst Loot Host_contig_land ///
	,efron shared(tc_ccode) frailty(gamma) nohr
	


	
********************************************************************************
**Figures 1 and 2
********************************************************************************

*Figure 1, Left Panel 
stset counter, failure(Death_Type==3) id(tc_ccode)
eststo: stcox Host_ThomRivals latentmean, efron ///
	tvc(Host_ThomRivals) texp(ln(_t)) ///
	shared(tc_ccode) frailty(gamma) nohr 

gen lndur = ln(_t)		
gen est = .
gen up = .
gen lw = . 
gen n = 0 if _n==1
replace n= n[_n-1] +.05 if n==.
replace n = . if n>4.15
	
	
local j = 1
forvalues i = 0(.05)4.15{
	quietly lincom [main]Host_ThomRivals + ([tvc]Host_ThomRivals*`i')
	replace est = 100*(exp(r(estimate))-1) if _n==`j'
	replace lw = 100*(exp(r(estimate)-1.96*r(se))-1) if _n==`j'
	replace up = 100*(exp(r(estimate)+1.96*r(se))-1) if _n==`j'
	local j = `j' + 1
}
graph twoway (hist lndur, freq col(gs14) yaxis(2) width(.05))  ///
	|| (hist lndur if Death_Type==3, col(red) freq yaxis(2) width(.05)) ///
	|| function y = 0, range(-.05 4.15)  clpattern(solid) clwidth(medium) clcolor(gs9) ///
	|| line lw n, clpattern(dash) clwidth(medium) clcolor(midblue) ///
	|| line up n, clpattern(dash) clwidth(medium) clcolor(midblue) ///			
	|| line est n, color(black) clwidth(medthick)  legend(off) clpattern(solid) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	xlabel(0 "1" .69314718 "2" 1.0986123 "3" 1.3862944 "4" 1.6094379 "5" ///
	1.9459101 "7" 2.3025851 "10" 2.7080502 "15" 2.9957323 "20" 3.4011974 "30" ///
	3.6888795 "40" 3.912023 "50" 4.0943446 "60", labsize(2.5) glcolor(white)) ///
	ylabel(-100 -75 -50 -25 0 25 50 75 100, nogrid  angle(horizontal) axis(1) labsize(2.5) glcolor(white)) ///
	ylabel(,  axis(2) angle(horizontal) tlcolor(gs11) labcolor(gs11) nogrid labsize(2) glcolor(white)) ///
	yscale(titlegap() range(-150 100) alt noext) ///
	yscale(titlegap() range(0 1100)  alt axis(2) noext lcolor(gs11)) ///
	xscale(titlegap(1.5) range(0 4.15) noext ) ///
	ytitle("Percent change in baseline "" hazard of peaceful reintegration", size(3) axis(1)) ///
	ytitle("Frequency", color(gs11) axis(2) size(3) margin(0 10 0 70) ) ///
	xtitle("Territorial Contender Age (Years)", size(3))	///
	title("", position(12) size(3)) ///
	text(-125 4.1 "Negotiated Settlements", color(red) size(3) placement(left)) ///
	scheme(s1mono) graphregion(fcolor(white) )  plotregion(lstyle(none) margin(l = 0 b = 1))
*graph export test.tif, width(4000) replace


 
*Figure 1, Right Panel 
stset counter, failure(Death_Type==3) id(tc_ccode)
eststo: stcox Host_ThomRivals latentmean Host_cinc_1000 ///
	num_tcs Mountains  ///
	,efron  shared(tc_ccode) frailty(gamma) nohr ///
		tvc(Host_ThomRivals) texp(ln(_t)) 

replace est = .
replace up = .
replace lw = . 
	
local j = 1
forvalues i = 0(.05)4.15{
	quietly lincom [main]Host_ThomRivals + ([tvc]Host_ThomRivals*`i')
	replace est = 100*(exp(r(estimate))-1) if _n==`j'
	replace lw = 100*(exp(r(estimate)-1.96*r(se))-1) if _n==`j'
	replace up = 100*(exp(r(estimate)+1.96*r(se))-1) if _n==`j'
	local j = `j' + 1
}
graph twoway (hist lndur, freq col(gs14) yaxis(2) width(.05))  ///
	|| (hist lndur if Death_Type==3, col(red) freq yaxis(2) width(.05)) ///
	|| function y = 0, range(-.05 4.15)  clpattern(solid) clwidth(medium) clcolor(gs9) ///
	|| line lw n, clpattern(dash) clwidth(medium) clcolor(midblue) ///
	|| line up n, clpattern(dash) clwidth(medium) clcolor(midblue) ///			
	|| line est n, color(black) clwidth(medthick)  legend(off) clpattern(solid) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	xlabel(0 "1" .69314718 "2" 1.0986123 "3" 1.3862944 "4" 1.6094379 "5" ///
	1.9459101 "7" 2.3025851 "10" 2.7080502 "15" 2.9957323 "20" 3.4011974 "30" ///
	3.6888795 "40" 3.912023 "50" 4.0943446 "60", labsize(2.5) glcolor(white)) ///
	ylabel(-100 -75 -50 -25 0 25 50 75 100, nogrid  angle(horizontal) axis(1) labsize(2.5) glcolor(white)) ///
	ylabel(,  axis(2) angle(horizontal) tlcolor(gs11) labcolor(gs11) nogrid labsize(2) glcolor(white)) ///
	yscale(titlegap() range(-150 100) alt noext) ///
	yscale(titlegap() range(0 1100)  alt axis(2) noext lcolor(gs11)) ///
	xscale(titlegap(1.5) range(0 4.15) noext ) ///
	ytitle("Percent change in baseline "" hazard of peaceful reintegration", size(3) axis(1)) ///
	ytitle("Frequency", color(gs11) axis(2) size(3) margin(0 10 0 70) ) ///
	xtitle("Territorial Contender Age (Years)", size(3))	///
	title("", position(12) size(3)) ///
	text(-125 4.1 "Negotiated Settlements", color(red) size(3) placement(left)) ///
	scheme(s1mono) graphregion(fcolor(white) )  plotregion(lstyle(none) margin(l = 0 b = 1))
*graph export test.tif, width(4000) replace
	
*Figure 2
stset counter, failure(Death_Type==3) id(tc_ccode)
stcox Host_ThomRivals latentmean Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Support Recognition ///
	Fragment xconst Mountains Loot Host_contig_land,efron  ///
	tvc(latentmean Host_contig_land xconst) ///
	texp(ln(_t)) nohr shared(tc_ccode) frailty(gamma)
	

replace est = .
replace up = .
replace lw = . 
local j = 1
forvalues i = 0(.05)4.15{
	quietly lincom .924*[main]latentmean + (.924*[tvc]latentmean*`i')
	replace est = 100*(exp(r(estimate))-1) if _n==`j'
	replace lw = 100*(exp(r(estimate)-1.96*r(se))-1) if _n==`j'
	replace up = 100*(exp(r(estimate)+1.96*r(se))-1) if _n==`j'
	local j = `j' + 1
}
graph twoway (hist lndur, freq col(gs14) yaxis(2) width(.05))  ///
	|| (hist lndur if Death_Type==3, col(red) freq yaxis(2) width(.05)) ///
	|| function y = 0, range(-.05 4.15)  clpattern(solid) clwidth(medium) clcolor(gs9) ///
	|| line lw n, clpattern(dash) clwidth(medium) clcolor(midblue) ///
	|| line up n, clpattern(dash) clwidth(medium) clcolor(midblue) ///			
	|| line est n, color(black) clwidth(medthick)  legend(off) clpattern(solid) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	xlabel(0 "1" .69314718 "2" 1.0986123 "3" 1.3862944 "4" 1.6094379 "5" ///
	1.9459101 "7" 2.3025851 "10" 2.7080502 "15" 2.9957323 "20" 3.4011974 "30" ///
	3.6888795 "40" 3.912023 "50" 4.0943446 "60", labsize(2.5) glcolor(white)) ///
	ylabel(-100 0 500 1000, nogrid  angle(horizontal) axis(1) labsize(2.5) glcolor(white)) ///
	ylabel(,  axis(2) angle(horizontal) tlcolor(gs11) labcolor(gs11) nogrid labsize(2) glcolor(white)) ///
	yscale(titlegap() range(-500 1000) alt noext) ///
	yscale(titlegap() range(0 1100)  alt axis(2) noext lcolor(gs11)) ///
	xscale(titlegap(1.5) range(0 4.15) noext ) ///
	ytitle("Percent change in baseline "" hazard of peaceful reintegration", size(3) axis(1)) ///
	ytitle("Frequency", color(gs11) axis(2) size(3) margin(0 10 0 60) ) ///
	xtitle("Territorial Contender Age (Years)", size(3))	///
	title("Effect of Human Rights Treaty Embeddedness" "On the Hazard of Peaceful Reintegration", position(12) size(3)) ///
	text(-350 4.1 "Negotiated Settlements", color(red) size(3) placement(left)) ///
	scheme(s1mono) graphregion(fcolor(white) )  plotregion(lstyle(none) margin(l = 0 b = 1)) 
*graph export test.tif, width(4000) replace

		

		
********************************************************************************
**Table 3 (Also Table C1) 
********************************************************************************
xtset(host_ccode)
*Table 3/C1, Model 1
xtlogit Peaceful Host_ThomRivals latentmean, fe
*Table 3/C1, Model 2
xtlogit Peaceful Host_ThomRivals latentmean i.year, fe
*Table 3/C1, Model 3
xtlogit Peaceful Host_ThomRivals latentmean ///
	Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Loot Support Recognition ///
	Fragment xconst   ///
	i.year, fe
*Table 3/C1, Model 4
xtlogit Forceful Host_ThomRivals latentmean, fe
*Table 3/C1, Model 5
xtlogit Forceful Host_ThomRivals latentmean i.year, fe
*Table 3/C1, Model 6
xtlogit Forceful Host_ThomRivals latentmean ///
	Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Loot Support Recognition ///
	Fragment xconst ///
	i.year, fe
*Table 3/C1, Model 7
xtlogit peaceforce Host_ThomRivals latentmean, fe
*Table 3/C1, Model 8
xtlogit peaceforce Host_ThomRivals latentmean i.year, fe
*Table 3/C1, Model 9
xtlogit peaceforce Host_ThomRivals latentmean ///
	Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Loot Support Recognition ///
	Fragment xconst  ///
	i.year, fe

	
	
	
********************************************************************************
**Appendix B: Descriptive Statistics
********************************************************************************
	
*Descriptive stats (Appendix B)
/*
The Death_Type indicator codes whether a TC
0 = survives
1 = is absorbed by another TC
2 = is forcefully reintegrated
3 = is peacefully reintegrated
4 = is promoted to soverign statehood
*/

*Generate a new indicator for descriptive stats, labeling surviving TCs in 2010
*as censored
gen Death_Type2 = Death_Type
replace Death_Type2 = 5 if Death_Type==0 & year==2010
tab Death_Type2
tabstat latentmean, by(Death_Type2)
tabstat Host_ThomRivals, by(Death_Type2)	 

estpost summarize Host_ThomRivals latentmean ///
	Host_cinc_1000 num_tcs ///
	Host_sf Host_Devel log_Pop ///
	Host_Alliances Loot Support Recognition ///
	Fragment xconst

	
	
	
********************************************************************************
**Table C2 (State Capacity)
********************************************************************************

xtset(host_ccode)
xtlogit Peaceful Host_ThomRivals latentmean myers ///
	i.year, fe
xtlogit Forceful Host_ThomRivals latentmean  myers ///
	i.year, fe
xtlogit peaceforce Host_ThomRivals latentmean myers ///
	i.year, fe
